import os

import Translation
import json
import time

# import demjson

'''
翻译json
type
   GG 谷歌翻译
   ZH 繁体简体转换
   TS Translate模块
'''


def translation(dic_json, tp):
    # 列表
    if isinstance(dic_json, list):
        # 遍历列表
        for i in range(len(dic_json)):
            # 当前项为字典
            if isinstance(dic_json[i], dict):
                for key in dic_json[i]:
                    # 键所对应的值是列表
                    if isinstance(dic_json[i][key], list):
                        # 递归
                        translation(dic_json[i][key], tp)
                    # 键所对应的值是字典
                    elif isinstance(dic_json[i][key], dict):
                        # 递归
                        translation(dic_json[i][key], tp)
                    # 字符串
                    else:
                        # 翻译前的值
                        print(dic_json[i][key])
                        # 翻译
                        if tp == 'GG':
                            dic_json[i][key] = Translation.UseGG(dic_json[i][key])
                            # 休眠，防止爬虫被检测
                            time.sleep(1.9)
                        elif tp == 'ZH':
                            dic_json[i][key] = Translation.UseZH(dic_json[i][key])
                        elif tp == 'TS':
                            dic_json[i][key] = Translation.UseTS(dic_json[i][key])
                        # 翻译后的值
                        print(dic_json[i][key])

            # 字符串
            elif isinstance(dic_json[i], str):
                print(dic_json[i])
                if tp == 'GG':
                    dic_json[i] = Translation.UseGG(dic_json[i])
                    # 休眠，防止爬虫被检测
                    time.sleep(1.9)
                elif tp == 'ZH':
                    dic_json[i] = Translation.UseZH(dic_json[i])
                elif tp == 'TS':
                    dic_json[i] = Translation.UseTS(dic_json[i])
                print(dic_json[i])
    # 字典
    else:
        for key in dic_json:
            # 键所对应的值是列表
            if isinstance(dic_json[key], list):
                # 递归
                translation(dic_json[key], tp)
            # 键所对应的值是字典
            elif isinstance(dic_json[key], dict):
                # 递归
                translation(dic_json[key], tp)
            else:
                # 翻译前的值
                print(dic_json[key])
                # 翻译
                if tp == 'GG':
                    dic_json[key] = Translation.UseGG(dic_json[key])
                    # 休眠，防止爬虫被检测
                    time.sleep(1.9)
                elif tp == 'ZH':
                    dic_json[key] = Translation.UseZH(dic_json[key])
                elif tp == 'TS':
                    dic_json[key] = Translation.UseTS(dic_json[key])
                # 翻译后的值
                print(dic_json[key])


files = []
dirs = []


# 遍历目录获取所有子目录以及子目录下的所有文件
def list_dir(start_dir):
    dir_res = os.listdir(start_dir)
    for path in dir_res:
        temp_path = start_dir + '/' + path
        if os.path.isfile(temp_path):
            files.append(temp_path)
        if os.path.isdir(temp_path):
            dirs.append(temp_path)
            list_dir(temp_path)


def transJSON(infile, outfile, tp):
    # 读取文件
    file = open(infile, encoding="utf-8")
    str1 = file.read()
    file.close()
    # print(str)

    data_json = json.loads(str1)
    # 不规则json字符串处理，使其可以成为对象
    # data_json = demjson.decode(str1, encoding="utf-8")
    # print(data_json)

    # 翻译
    translation(data_json, tp)

    # 写入
    with open(outfile, "w", encoding="utf-8") as fp:
        fp.write(json.dumps(data_json, ensure_ascii=False, indent=4))


def transENUS():
    # 遍历指定文件夹及子文件
    list_dir("en-US")
    for infile in files:
        # 输出的文件夹
        out_dirs = infile.replace('en-US/', 'zh-CN/').replace('/en-US.locPak', '')
        if not os.path.exists(out_dirs):
            os.makedirs(out_dirs)
        # 输出文件
        outfile = infile.replace('en-US/', 'zh-CN/')
        if not os.path.exists(outfile):
            # 翻译文件
            try:
                transJSON(infile, outfile, "ZH")
            except Exception as r:
                print("error: ", r)


if __name__ == '__main__':
    transJSON("test/demo.json", "test/demo.json", "GG")
